package org.etom.impl.connector.datatypes;

import java.util.Map;

import org.etom.connector.datatypes.IMoodleCourse;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.connector.datatypes.IMoodleUser;
import org.etom.connector.service.ConnectorException;
import org.etom.impl.connector.util.XmlRpcClientWrapper;

/**
 * Implementation of the Moodle course.
 * The instances of that class are particularly mutable. 
 */
public class MoodleCourseImpl implements IMoodleCourse {
  
  private boolean areRequestPropsSet;
  private boolean isUserSet;
  private String fullname;
  private Long id;
//  private IMoodleModule[] modules;
  private IMoodleUser user;
//  private int modulesType;
  private XmlRpcClientWrapper xmlRpcClientWrapper;
  private Map<String, String> requestProps;
  
  /**
   * Construct a new Moodle course.
   * 
   * @param fullname - the full name of the course
   * @param id - the identifier of the course
   * @param xmlRpcClientWrapper - xml rpc wrapper
   */
  public MoodleCourseImpl(String fullname, Long id, XmlRpcClientWrapper xmlRpcClientWrapper) {
    this.fullname = fullname;
    this.id = id;
    this.xmlRpcClientWrapper = xmlRpcClientWrapper;
    areRequestPropsSet = false;
    isUserSet = false;
  }

  /**
   * The method sets the request properties.
   * Those properties are used in the connection to Moodle server.
   * 
   * @param requestProps - the request properties
   */
  public void setRequestProperties(Map<String, String> requestProps) {
    this.requestProps = requestProps;
    areRequestPropsSet = true;
  }
  
  /**
   * The method sets the moodle user.
   * The course belongs to that user.
   * 
   * @param user - moodle user which course is this
   */
  public void setUser(IMoodleUser user) {
    this.user = user;
    isUserSet = true;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleCourse#getFullName()
   */
  public String getFullName() {
    checkState();
    return fullname;
  }

  /**
   * @see org.etom.connector.datatypes.IMoodleCourse#getId()
   */
  public Long getId() {
    checkState();
    return id;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleCourse#getModules(int)
   */
  public IMoodleModule[] getModules(int type) throws ConnectorException {
    checkState();
//    if ((modules == null) || (modulesType != type)) {
//      if ((type & IMoodleModule.RESOURCE) != 0) {
//        modules = xmlRpcClientWrapper.getMoodleCourseModules(this, IMoodleModule.RESOURCE, requestProps);
//      } else if ((type & IMoodleModule.ASSIGNMENT) != 0) {
//        modules = xmlRpcClientWrapper.getMoodleCourseModules(this, IMoodleModule.ASSIGNMENT, requestProps);
//      }
//      modulesType = type;
//    }
//    return modules;
    if ((type & IMoodleModule.RESOURCE) != 0) {
      return xmlRpcClientWrapper.getMoodleCourseModules(this, IMoodleModule.RESOURCE, requestProps);
    } else if ((type & IMoodleModule.ASSIGNMENT) != 0) {
      return xmlRpcClientWrapper.getMoodleCourseModules(this, IMoodleModule.ASSIGNMENT, requestProps);
    }
    throw new ConnectorException("The Moodle module type is unknown! Type: " + type,
        ConnectorException.ILLEGAL_ARGUMENT);
  }

  /**
   * @see org.etom.connector.datatypes.IMoodleCourse#getUser()
   */
  @Override
  public IMoodleUser getUser() {
    checkState();
    return user;
  }  
  
  /*
   * The state 
   */
  private void checkState() {
    if (!areRequestPropsSet)
      throw new IllegalStateException(
          "The mandarory properties of the moodle course are not set!");
    if (!isUserSet)
      throw new IllegalStateException(
          "The mandarory user of the moodle course is not set!");
  }
  
  @Override
  public boolean equals(Object other) {
    if (!(other instanceof IMoodleCourse)) {
      return false;
    }
    IMoodleCourse otherCourse = (IMoodleCourse) other;
    return this.id.equals(otherCourse.getId());
  }

  @Override
  public int hashCode() {
    return this.id.hashCode();
  }
  
}
